/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.data;

import datamodel.Category;
import datamodel.SystemAdmin;
import datamodel.SystemCustomer;
import datamodel.SystemUser;
import ejb.CategoryManager;
import ejb.UserManager;
import helpers.Hasher;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;

/**
 *
 * @author eXtreme
 */
@ManagedBean(name = "user")
@RequestScoped
public class UserBean {
    
    private SystemUser user;
    private String userName;
    private String userSurname;
    private String userPassword;
    private String userEmail;    
    private SystemUser loggedUser;
    @EJB
    private UserManager um;
    @EJB
    private CategoryManager cm;
    
    public UserBean() {
        
    }

    public SystemUser getUser() {
        return user;
    }

    public void setUser(SystemUser user) {
        this.user = user;
    }
    
    public String getUserEmail() {
        return userEmail;
    }
    
    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }
    
    public String getUserName() {
        return userName;
    }
    
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public String getUserPassword() {
        return userPassword;
    }
    
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    
    public String getUserSurname() {
        return userSurname;
    }
    
    public void setUserSurname(String userSurname) {
        this.userSurname = userSurname;
    }
    
    public SystemUser getLoggedUser() {
        return loggedUser;
    }
    
    public List<String> getUserRoles() {
        return um.getUserRoleTypes();
    }
    
    public void genUser() {
        saveNewUser("Petr", "Hejhal", "admin", "none", true);
        saveNewUser("Petr", "Smrček", "customer", "none", false);
    }
    
    public String editUser(SystemUser u) {
        this.user = u;
        
        return "userDetail?faces-redirect=true";
    }
    
    public void saveNewUser(String name, String surname, String email, String password, boolean admin) {
        
        if(um.exists(email)){
            System.out.println("Uživatel s tímto emailem již existuje");
            return;
        }
        
        SystemUser temp;
        password = Hasher.sha256(password);
        if (admin) {
             temp = new SystemAdmin(name, surname, email, password);
        } else {
             temp = new SystemCustomer(name, surname, email, password);
        }
        um.saveUser(temp);
    }
    
    public List<SystemUser> getAllUsers() {
        return um.getAllUsers();
    }
    
    public void removeUser(SystemUser user){
        System.out.println("Mazu uzivatele" + user.toString());
        um.remove(user);
    }
    
}
